//
//  WBMainViewController.swift
//  weibo_swift
//
//  Created by qiuming on 16/9/18.
//  Copyright © 2016年 qiuming. All rights reserved.
//

import UIKit

class WBMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewControllers()
        
        setupComposedButton()

        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        tabBar.bringSubview(toFront: composedButton)
    }
    
    @objc func clickComposeBtn() {
        
        print("加号按钮点击了")
    }
    
    
    lazy var composedButton: UIButton = UIButton(imageName: "tabbar_compose_icon_add", backImageName: "tabbar_compose_button")
    

    
}

// MARK: - 界面设置
extension WBMainViewController {
    
    func setupComposedButton() {
        
        tabBar.addSubview(composedButton)
        
        let count = childViewControllers.count
        
        let w = tabBar.bounds.width / CGFloat(count) - 1
        
        composedButton.frame = tabBar.bounds.insetBy(dx: 2 * w, dy: 0)
        
        composedButton.addTarget(self, action: #selector(WBMainViewController.clickComposeBtn), for: .touchUpInside)
        
        
    }
    
    func addChildViewControllers() {
        
        tabBar.tintColor = UIColor.orange
        
        addChildViewController(vc: WBHomeViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(vc: WBMessageViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController())
        
        addChildViewController(vc: WBDiscoveryViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(vc: WBProfileViewController(), title: "我", imageName: "tabbar_profile")
        
    }
    
    private func addChildViewController(vc: UIViewController, title: String, imageName: String) {
        
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
        
    }
    
    
}


























